-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(common): add tag! macro #453
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## julio/APMSP-1004-add-metrics #453 +/- ##
================================================================
- Coverage 68.23% 68.05% -0.18%
================================================================
Files 193 192 -1
Lines 24899 24746 -153
================================================================
- Hits 16989 16841 -148
+ Misses 7910 7905 -5
|
dbb87cd
to
711e6e2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
b80b957
to
4e44e7e
Compare
This does a few things: 1. Creates a `tag!` macro which does compile-time checking for some invariants. 2. Changes a `Tag` to use `Cow<'static, str>` instead of a `String`. As libdatadog usage has grown, we have more Rust users of the lib and therefore more static Rust strs we can trust at compile-time and avoid runtime allocations. 3. Makes `Tag::from_value` private. Use `tag!` or `Tag::new`.
4e44e7e
to
2c5d453
Compare
This is cool. While reviewing #450 I was thinking to myself "man, it sucks we can't know these tags are valid at compile time". |
ddcommon/src/tag.rs
Outdated
/// the <KEY>:<VALUE> format are preferred. | ||
pub fn from_value<'a, IntoCow>(chunk: IntoCow) -> Result<Self, Cow<'static, str>> | ||
/// Validates a tag. | ||
fn from_value<'a, IntoCow>(chunk: IntoCow) -> Result<Self, Cow<'static, str>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be try_from
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would make it a public since it's a trait impl. It's an implementation detail used by parse_tags
and new
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice!
I wasn't completely sure what this would end up as in the generated binary, so I dug into it with readelf
and objdump
and it's indeed reusing constant tag data defined in multiple places.
What does this PR do?
This does a few things:
tag!
macro which does compile-time checking for some invariants.Tag
to useCow<'static, str>
instead of aString
.Tag::from_value
private. Usetag!
if the key and value are known at compile-time. If not, useTag::new
.Tag::new
returns ananyhow::Result<Tag>
now instead of aResult<Tag, Cow<_>>
.Motivation
As libdatadog usage has grown, we have more Rust users of the lib and therefore more static Rust strs we can trust at compile-time and avoid runtime allocations.
Additional Notes
This was previously stacked on another PR, but not anymore.
How to test the change?
Everything should run as normal. It's recommended to use
tag!
overTag::new
if the key and value are both static strs.